package cn.dankal.flyaudio.activity.search;

import android.annotation.SuppressLint;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;

import api.ActivityServiceFactory;
import cn.dankal.basiclib.base.BaseRvFragmentImp;
import cn.dankal.basiclib.base.BaseStateView;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewAdapter;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewPresenter;
import cn.dankal.basiclib.base.recyclerview.OnRvItemClickListener;
import cn.dankal.basiclib.protocol.MainProtocol;
import cn.dankal.basiclib.rx.AbstractStateSubscriber;
import cn.dankal.basiclib.util.SoftKeyboardUtil;
import cn.dankal.business.pojo.ActivityBean;
import cn.dankal.business.pojo.BaseBeanList;
import cn.dankal.flyaudio.home.ActivityAdapter;

/**
 * @author ricky
 * @date 2018/8/14 0014
 */
@SuppressLint("ValidFragment")
public class SearchResultFragment extends BaseRvFragmentImp<ActivityBean> implements BaseStateView {
    private String searchName = "";


    /**
     * 更新搜索内容,回调搜索接口
     *
     * @param searchName
     */
    public void setSearchName(String searchName) {
        this.searchName = searchName;
        showLoading();
        mPresenter.requestData("1");
        SoftKeyboardUtil.hideSoftInput(getContext());
    }


    @Override
    public void initComponents() {
        super.initComponents();
        swipeToLoadLayout.setLoadMoreEnabled(false);
    }


    @Override
    public BaseRecyclerViewPresenter<ActivityBean> getPresenter() {
        return new BaseRecyclerViewPresenter<ActivityBean>() {
            @Override
            public void requestData(String times) {
                ActivityServiceFactory.activity_list(searchName, times, PAGE_SIZE)
                        .subscribe(new AbstractStateSubscriber<BaseBeanList<ActivityBean>>(SearchResultFragment.this) {

                            @Override
                            public void onNext(BaseBeanList<ActivityBean> activityBeanBaseBeanList) {
                                super.onNext(activityBeanBaseBeanList);
                                render(activityBeanBaseBeanList.list);
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
                                showRetry();
                            }
                        });
            }
        };
    }

    @Override
    public BaseRecyclerViewAdapter getAdapter() {
        ActivityAdapter activityAdapter = new ActivityAdapter();
        activityAdapter.setOnRvItemClickListener(new OnRvItemClickListener<ActivityBean>() {
            @Override
            public void onItemClick(View v, int position, ActivityBean data) {
                ARouter.getInstance().build(MainProtocol.ACTIVITY_DETAIL)
                        .withString("uuid",data.uuid)
                        .navigation();
            }
        });
        return activityAdapter;
    }
}
